﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_cardmagic", Package.CardPack)
extension.extensionName = "moepack"

Fk:loadTranslationTable{
  ["moepack_cardmagic"] = "萌包卡牌-魔法",
}

local U = require "packages/utility/utility"

fk.CharmDamage = "charm_damage"
Fk:addDamageNature(fk.CharmDamage, "charm_damage", true)

local charm_skill = fk.CreateActiveSkill{
  name = "charm_skill",
  prompt = "#charm_skill",
  -- 由于增减目标只考虑modTargetFilter，故modTargetFilter只应判断距离限制，而次数限制应在targetFilter中判断
  mod_target_filter = Util.TrueFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    if not Util.TargetFilter(self, to_select, selected, selected_cards, card, extra_data, player) then return end
    local to = Fk:currentRoom():getPlayerById(to_select)
    return
    (
      #selected > 0 or -- ?为啥
      (extra_data and extra_data.bypass_times) or
      self:withinTimesLimit(player, Player.HistoryTurn, card, "charm", to)
    )
  end,
  target_num = 1,
  max_turn_use_time = 1,
  can_use = function(self, player, card, extra_data)
    if player:prohibitUse(card) then return end
    return (extra_data and extra_data.bypass_times) or
      table.find(Fk:currentRoom().alive_players, function(to)
        return self:withinTimesLimit(player, Player.HistoryTurn, card, "charm", to)
      end)
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local player = room:getPlayerById(effect.from)
    local cardResponded = room:askForResponse(to, "charm", nil, nil, true, nil, effect)
    if cardResponded then
      room:responseCard({
        from = effect.to,
        card = cardResponded,
        responseToEvent = effect,
      })
    else
      room:damage({
        from = room:getPlayerById(effect.from),
        to = to,
        card = effect.card,
        damage = 1,
        damageType = fk.CharmDamage,
        skillName = self.name,
      })
    end
  end,
}

local charm = fk.CreateBasicCard{
  name = "charm",
  suit = Card.Spade,
  number = 1,
  skill = charm_skill,
  is_damage_card = true,
} 

for suit = 1, 4 do
  for number = 1, 13 do
    extension:addCard(charm:clone(suit, number))
  end
end

-- 无视护甲巧思
local charm_trigger = fk.CreateTriggerSkill{
  name = "charm_trigger",
  global = true,
  refresh_events = {fk.BeforeHpChanged},
  can_refresh = function(self, event, target, player, data)
    return player == target and data.damageEvent and data.damageEvent.damageType == fk.CharmDamage
  end,
  on_refresh = function (self, event, target, player, data)
    data.num = data.num - data.shield_lost
    data.shield_lost = 0
    player.room:broadcastPlaySound("./packages/moepack/audio/card/charm_damage")
    player.room:delay(200)
  end,
}
Fk:addSkill(charm_trigger)

Fk:loadTranslationTable{
  ["charm"] = "法",
	[":charm"] = "基本牌<br /><b>时机</b>：出牌阶段（每回合限一次）<br /><b>目标：</b>一名角色<br /><b>效果：</b>目标角色需打出一张【法】，否则你对其造成1点法术伤害（无视护甲，可被传导）。",
  ["charm_skill"] = "法",
  ["#charm_skill"] = "对一名角色造成1点法术伤害",
  ["charm_trigger"] = "法",
  ["charm_damage"] = "法术伤害",
}

local wish_fulfill_skill = fk.CreateActiveSkill{
  name = "wish_fulfill_skill",
  prompt = "#wish_fulfill_skill",
  mod_target_filter = Util.TrueFunc,
  can_use = function(self, player, card)
    return not player:isProhibited(player, card)
  end,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.to)
    local cardType = room:askForChoice(player, {"basic", "trick", "equip"}, self.name)
    local names, ids = {}, {}
    local piles = table.connect(room.draw_pile, room.discard_pile)
    while #piles > 0 and #names < 5 do
      local id = table.remove(piles, math.random(#piles))
      if Fk:getCardById(id):getTypeString() == cardType then
        if table.insertIfNeed(names, Fk:getCardById(id).name) then
          table.insert(ids, id)
        end
      end
    end
    if #names == 0 then return end
    local name = U.askForChooseCardNames(room, player, names, 1, 1, self.name)[1]
    room:moveCardTo(ids[table.indexOf(names, name)], Player.Hand, player, fk.ReasonJustMove, self.name, nil, false)
  end
}
local wish_fulfill = fk.CreateTrickCard{
  name = "wish_fulfill",
  suit = Card.Spade,
  number = 13,
  skill = wish_fulfill_skill,
}

extension:addCards({
  wish_fulfill,
  wish_fulfill:clone(Card.Heart, 12),
})


Fk:loadTranslationTable{
  ["wish_fulfill"] = "心想事成",
  [":wish_fulfill"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：你<br /><b>效果</b>：目标角色选择一种类型，从此类型随机五种牌名选一张获得。",
  ["wish_fulfill_skill"] = "心想事成",
  ["#wish_fulfill_skill"] = "选择一种类型，再从5种此类型的牌挑选一张获得",
}

local moon_stick_skill = fk.CreateTriggerSkill{
  name = "#moon_stick_skill",
  anim_type = "drawcard",
  attached_equip = "moon_stick",
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash" 
    and data.card.number > 0 and data.card.suit ~= Card.NoSuit
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getCardsFromPileByRule("charm|"..data.card.number.."|"..data.card:getSuitString(), 1, "allPiles")
    if #ids > 0 then
      room:obtainCard(player, ids, true, fk.ReasonJustMove, player.id, self.name)
    end
  end,
}

Fk:addSkill(moon_stick_skill)

local moon_stick = fk.CreateWeapon{
  name = "moon_stick",
  suit = Card.Spade,
  number = 7,
  attack_range = 2,
  equip_skill = moon_stick_skill,
}

extension:addCards({
  moon_stick,
})

Fk:loadTranslationTable{
  ["moon_stick"] = "魔月杖",
  [":moon_stick"] = "装备牌·武器<br /><b>攻击范围</b>：２<br /><b>武器技能</b>：锁定技，每当你使用【杀】时，获得一张与之点数和花色均相同的【法】。",
  ["#moon_stick_skill"] = "魔月杖",
}






return extension
